package cn.tedu.mall.search.controller;

import cn.tedu.mall.common.restful.JsonPage;
import cn.tedu.mall.common.restful.JsonResult;
import cn.tedu.mall.pojo.search.entity.SpuEntity;
import cn.tedu.mall.search.service.ISearchService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api(tags = "搜索商品模块")
@RequestMapping("/search")
public class SearchController {

    @Autowired
    private ISearchService searchService;

    @PostMapping("/load/data")
    @ApiOperation("es加载spu所有数据")
    public JsonResult loadData(){
        searchService.loadSpuByPage();
        return JsonResult.ok("运行无异常");
    }

    @GetMapping("")
    @ApiOperation("根据关键字查询数据")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "关键词",name = "keyword",required = true,dataType = "string"),
            @ApiImplicitParam(value = "页码",name = "page",dataType = "int"),
            @ApiImplicitParam(value = "每页条数",name = "pageSize",dataType = "int")
    })
    public JsonResult<JsonPage<SpuEntity>> searchByKeyword(String keyword, Integer page,Integer pageSize){
        JsonPage<SpuEntity> list = searchService.search(keyword, page, pageSize);
        return JsonResult.ok(list);
    }

}
